package com.example.demo.controller;

import com.example.demo.entity.*;
import com.example.demo.result.R;
import com.example.demo.result.Renum;
import com.example.demo.service.FunctionPointService;
import com.example.demo.service.ProjectService;
import com.example.demo.service.TaskService;
import com.example.demo.utils.RUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/Task")
public class TaskController {
    @Autowired
    TaskService taskService;
    @Autowired
    ProjectService projectService;
    @Autowired
    FunctionPointService functionPointService;

    @CrossOrigin
    @ResponseBody
    @RequestMapping("/addTask")
    public R addTask(String taskName, String taskContent, String openTime , String completeTime,
                     Integer chargeId , Integer taskPro, Integer publisherId, Integer taskState,
                     String taskType, String taskPriority, Integer taskMilestone,Integer functionId) throws ParseException {
        java.text.SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = formatter.parse(openTime);
        Date date1 = formatter.parse(completeTime);
        Task task = new Task();
        task.setTaskName(taskName);
        task.setTaskContent(taskContent);
        task.setTaskOpenTime(date);
        task.setTaskCompleteTime(date1);
        task.setTaskChargeId(chargeId);
        task.setTaskPro(taskPro);
        task.setTaskPublisherId(publisherId);
        task.setTaskState(taskState);
        task.setTaskType(taskType);
        task.setTaskPriority(taskPriority);
        task.setTaskMilestoneId(taskMilestone);
        task.setTaskFunctionId(functionId);
        if(taskService.addTask(task)>0)
            return RUtils.success(task);
        return RUtils.error(Renum.ERROR);
    }

    @CrossOrigin
    @ResponseBody
    @RequestMapping("/addTaskUser")
    public R addTaskUser (Integer taskId, Integer [] userId){
        boolean flag=true;
        for(int i = 0 ; i < userId.length ; i++){
            TaskMember taskMember = new TaskMember();
            taskMember.setTaskMemTaskId(taskId);
            taskMember.setTaskMemUserId(userId[i]);
            if(taskService.addTaskUser(taskMember)>i){
                flag = true;
            }else {
                flag = false;
            }
        }
        if (flag=true){
            return RUtils.success(Renum.SUCCESS);
        }else {
            return RUtils.error(Renum.ERROR);
        }
    }

    @CrossOrigin
    @ResponseBody
    @RequestMapping("/selectTask")
    //返回任务具体信息
    public List<Map<String, String
            >> selectTaskInformationList (Integer userId){
        List<Task> tasks = new ArrayList<>();
        tasks = taskService.getTask(userId);
        List<Map<String , String>> tasksInfo = new ArrayList<>();
        for(Task iterator : tasks){
            Project project = projectService.getInfo(iterator.getTaskPro());
            FunctionPoint fp = functionPointService.getFpInfo(iterator.getTaskFunctionId());
            System.out.println("项目名称："+project.getProName()+"任务Id:"+iterator.getTaskId());
            Map<String, String> taskInfo = new HashMap<String, String>();
            taskInfo.put("taskId",iterator.getTaskId().toString());
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String taskOpenTime = formatter.format(iterator.getTaskOpenTime());
            taskInfo.put("taskOpenTime",taskOpenTime);
            taskInfo.put("taskName", iterator.getTaskName());
            taskInfo.put("isProName", project.getProName());
            taskInfo.put("taskType",iterator.getTaskType());
            String taskCompleteTime = formatter.format(iterator.getTaskCompleteTime());
            taskInfo.put("taskCompleteTime", taskCompleteTime);
            taskInfo.put("taskFunctionId",fp.getFunctionName());
            tasksInfo.add(taskInfo);
        }
        return tasksInfo;
    }

    @CrossOrigin
    @ResponseBody
    @RequestMapping("/selectChild")
    //返回子任务
    public  List<Task> selectChildList(Integer taskId){
        return taskService.getChildTask(taskId);
    }

    @CrossOrigin
    @ResponseBody
    @RequestMapping("/selectFather")
    //返回子任务
    public  List<Task> selectFatherList(Integer taskId){
        return taskService.getFatherTask(taskId);
    }


    @CrossOrigin
    @ResponseBody
    @RequestMapping("/changeTaskInformation")
    //更改任务信息
    public R changeTaskInformation(Integer taskId, String taskName, String taskContent, Date taskOpenTime, Date taskCompleteTime, Integer taskFunctionId){
        taskService.changeTask(taskId,taskName,taskContent,taskOpenTime,taskCompleteTime,taskFunctionId);
        return RUtils.success(Renum.SUCCESS);
    }

}
